<template>
  <div class="app-container">
    <div class="customContainer">
      <!-- 搜索头 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <el-form-item>
          <!-- <el-select placeholder="选择品牌" v-model="formInline.brand" @change="brandChange">
                        <el-option v-for="item in brandList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>-->
          <el-select filterable placeholder="选择门店" v-model="formInline.store_id">
            <el-option v-for="item in storeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input v-model="formInline.gift_id" placeholder="请输入礼品编号/礼品名称"></el-input>
        </el-form-item>
        <el-form-item>
          <dj-button size="small" @search="searchClick">
            <template #default>搜索</template>
          </dj-button>
        </el-form-item>
      </el-form>

      <!-- 表格 -->
      <el-table highlight-current-row :data="list" v-loading.body="listLoading" border size="small">
        <el-table-column align="center" label="图片" width="80">
          <template slot-scope="scope">
            <img :src="scope.row.thumb" width="60" height="60" alt />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="gift_name" label="商品名称"></el-table-column>
        <el-table-column align="center" prop="gift_id" label="新商品编号"></el-table-column>
        <el-table-column align="center" prop="no_obsolete" label="神采编号"></el-table-column>
        <el-table-column align="center" prop="gift_type" label="商品类别"></el-table-column>
        <el-table-column align="center" prop="num" label="商品库存"></el-table-column>
        <el-table-column align="center" label="成本价">
          <template slot-scope="scope">{{scope.row.money_cost/1000000|keepTwoNum}}</template>
        </el-table-column>
        <el-table-column align="center" label="员工内买价">
          <template slot-scope="scope">{{scope.row.bug_money/1000000|ceil}}</template>
        </el-table-column>
        <el-table-column align="center" label="状态">
          <template slot-scope="scope">
            <el-button v-if="scope.row.is_buy==0" size="mini" @click="on_off_line(scope.row)">已下架</el-button>
            <el-button v-else size="mini" type="success" @click="on_off_line(scope.row)">已上架</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 弹框 -->
    <el-dialog
      :close-on-click-modal="false"
      width="370px"
      title="员工内买价修改"
      :visible.sync="multipleDialog"
    >
      <el-form :model="multipleForm" ref="multipleForm" :inline="true" :rules="rules">
        <el-form-item label="内买价：成本价" prop="proportion">
          <el-input size="mini" v-model="multipleForm.proportion"></el-input>
        </el-form-item>
        <el-form-item>倍</el-form-item>
      </el-form>
      <span class="danger">*</span> 价格修改后作用于内买模块所有商品
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveClick('multipleForm')" size="small">确认修改</el-button>&nbsp;
      </div>
    </el-dialog>
    <div class="bottom_box">
      <el-pagination
        class="pull-left"
        background
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="20"
        layout="prev, pager, next, jumper"
        :total="totalDataNumber"
      ></el-pagination>
      <el-button class="pull-right" type="primary" size="small" @click="editMoney">修改内买价</el-button>
    </div>
  </div>
</template>
<script>
import {
  getList,
  getPage,
  getBrandList,
  getStoreList,
  on_off,
  save,
} from "@/api/member/staff_buy";
import { multiple0to10 } from "@/utils/validate";
export default {
  data() {
    const validateVal = (rule, value, callback) => {
      if (!multiple0to10(value)) {
        callback(new Error("输入0-10,最多能保留两位小数"));
      } else {
        callback();
      }
    };
    return {
      list: null,
      listLoading: false,
      multipleDialog: false,
      currentPage: 1,
      totalDataNumber: 1,
      multipleForm: {
        proportion: "",
        store_id: "",
        type: "2",
      },
      storeList: null,
      formInline: {
        store_id: "",
        gift_id: "",
      },
      rules: {
        proportion: [
          { required: true, trigger: "blur", validator: validateVal },
        ],
      },
    };
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      //当进入组件之前，执行 beforRouteEnter 路由钩子函数 ,//参数vm就是当前组件的实例。
      if (!vm.storeList) {
        vm.getStore();
      }
    });
  },
  methods: {
    // 提交
    saveClick(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.multipleForm.store_id = this.formInline.store_id;
          save(this.multipleForm).then((response) => {
            this.$message({
              type: response.code == 1 ? "success" : "error",
              message: response.message,
            });
            this.fetchData(this.formInline, this.currentPage);
            this.multipleDialog = false;
          });
        }
      });
    },
    on_off_line(row) {
      let text = row.is_buy == 1 ? "确认下架" : "确认上架";
      this.$confirm(text, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        on_off({
          gift_id: row.gift_id,
          store_id: row.store_id,
          buy: row.is_buy == 1 ? 0 : 1,
        }).then((response) => {
          this.$message({
            type: response.code == 1 ? "success" : "error",
            message: response.message,
          });
          if (response.code == 1) {
            row.is_buy = row.is_buy == 1 ? 0 : 1;
          }
        });
      });
    },
    editMoney() {
      if (this.$refs["multipleForm"] !== undefined) {
        this.$refs["multipleForm"].resetFields();
      }
      // 查看接口？
      save({ type: 1, store_id: this.formInline.store_id }).then((response) => {
        this.multipleForm.proportion = response;
      });
      this.multipleDialog = true;
    },
    // getBrand() {
    //     getBrandList({ org: 1, category_id: 307 }).then(response => {
    //         this.brandList = response;
    //         this.formInline.brand = response[0].id
    //         this.getStore()
    //     });
    // },
    // brandChange() {
    //     // this.formInline.store_id = '';
    //     this.getStore();
    // },
    getStore() {
      getStoreList({ category_id: 307 }).then((response) => {
        this.storeList = response;
        let data = response.filter(
          (res) => res.id == this.$store.getters.optionId
        );
        if (data.length > 0) {
          this.formInline.store_id = data[0].id;
        }
        if (this.list == null) {
          this.fetchData();
        }
      });
    },
    // 搜索
    searchClick() {
      this.fetchData();
    },
    // 列表获取
    fetchData() {
      this.listLoading = true;
      this.currentPage = 1;
      getPage(this.formInline)
        .then((response) => {
          this.totalDataNumber = response * 20;
          getList(this.formInline).then((response) => {
            this.list = response;
            this.listLoading = false;
          });
        })
        .catch((res) => {
          this.listLoading = false;
        });
    },
    // 分页
    handleCurrentChange(val) {
      this.listLoading = true;
      getList(this.formInline, val).then((response) => {
        this.list = response;
        this.listLoading = false;
      });
    },
  },
};
</script>